/*************************************************************************
	> File Name: add.c
	> Author: WangYiFei
	> Mail: 824537320@qq.com
	> Created Time: Mon 11 Jan 2021 05:10:09 PM CST
 ************************************************************************/

#include "head.h"

#define INS 2

int main(int argc, char **argv) {
    int opt;
    int fd;
    int start, end;
    char buff[1024];
    pid_t pid;
    int waitCnt = 0;
    int sum = 0;
    while ((opt = getopt(argc, argv, "s:e:")) != -1) {
        switch (opt) {
            case 's':
                start = atoi(optarg);
                // printf("s : %d\n", start);
                break;
            case 'e':
                end = atoi(optarg);
                // printf("e : %d\n", end);
                break;
            default:
                printf("error\n");
                break;
        }
    }
    if ((fd = open("./add.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
    // if ((fd = open("./a.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
        perror("open()");
        exit(1);
    }
    memset(buff, 0, sizeof(buff));
    sprintf(buff, "%d,%d", start, start + 1);
    if (write(fd, buff, sizeof(buff)) < 0) {
        perror("write()");
        exit(1);
    }
    close(fd);
    
    for (int i = 0; i < INS; i++) {
        if ((pid = fork()) < 0) {
            perror("fork()");
            exit(1);
        }
        if (pid == 0) {
            while (1) {
                int fd;
                int a, b;
                if ((fd = open("./add.txt", O_RDWR)) < 0) {
                    // perror("open()");
                    // exit(1);
                    continue;
                }
                memset(buff, 0, sizeof(buff));
                if (flock(fd, LOCK_EX) == -1) {
                    perror("flock(LOCK_EX)");
                    exit(1);
                }
                if (read(fd, buff, sizeof(buff)) > 0) {
                    if (buff[0] == 'e') {
                        close(fd);
                        exit(0);
                    } else {
                        sscanf(buff, "%d,%d", &a, &b); 
                    }
                }
                sum = a + b;
                if (b == end) {
                    // printf("end\n");
                    sprintf(buff, "%s,%d", "end", sum);
                } else {
                    // printf("%d\n", sum);
                    sprintf(buff, "%d,%d", sum, b + 1);
                }
                lseek(fd, 0, SEEK_SET);
                if (write(fd, buff, sizeof(buff)) < 0) {
                    perror("write()");
                    exit(1);
                }
                if (flock(fd, LOCK_UN) < 0) {
                    perror("flock(LOCK_UN)");
                    exit(1);
                }
                close(fd);
            }
        }
    }
    while (wait(NULL) != -1) {
        waitCnt++;
        if (waitCnt == INS) break;
    }
    if ((fd = open("./add.txt", O_RDWR)) < 0) {
        perror("open()");
        exit(1);
    }
    memset(buff, 0, sizeof(buff));
    if (read(fd, buff, sizeof(buff)) > 0) {
        sscanf(buff, "end,%d", &sum);
        printf("%d\n", sum);
    }
    close(fd);
    if (remove("./add.txt") == -1) {
        perror("remove()");
        exit(1);
    }


    return 0;
}
